﻿using Shop.Core.Authorize;
using Shop.Core.Repository;
using Shop.IServices.Admin;
using Shop.Model.Entity.Jur;
using Shop.Model.View.Admin.Jur;
using SqlSugar;

namespace Shop.Services.Admin;

public class UserRoleService: IUserRoleService, IPermissionService
{
    private readonly RepositoryBase<JurRole> _roleRepository;

    public UserRoleService(RepositoryBase<JurRole> roleRepository)
    {
        _roleRepository = roleRepository;
    }
    
    /// <summary>
    /// 获取角色权限
    /// </summary>
    /// <returns></returns>
    public async Task<List<PermissionItem>> GetRoleMenu()
    {
        return await _roleRepository.Queryable<JurRoleMenu, JurMenu>((rm, m) => new object[]
                {
                    JoinType.Inner, rm.MenuId == m.Id
                })
            .Where((rm, m) => m.State == 0)
            .Select((rm, m) => new PermissionItem
            {
                Role = rm.RoleId.ToString(),
                RouteUrl = m.Path,
                Url = m.Component
            })
            .ToListAsync();
    }
}